package com.example.hzz.geoquize;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizeActivity extends AppCompatActivity {
    private static String TAG = "QuizeActivity";
    private static final String KEY_INDEX = "index";
    private static final String KEY_CHEATER = "cheater";
    private Button button1, button2;
    private Button button3, button4;
    private TextView mQuestionView;
    private boolean mIsCheater = false;
    private int mCurrenId = 0;
    private TrueFalse[] mQuestionBank = new TrueFalse[]{
            new TrueFalse(R.string.question_city, false),
            new TrueFalse(R.string.question_ocean, true),
            new TrueFalse(R.string.question_mideast, false),
            new TrueFalse(R.string.question_africa, false),
            new TrueFalse(R.string.question_americas, true),
            new TrueFalse(R.string.question_asia, true),


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mCurrenId = savedInstanceState.getInt(KEY_INDEX, 0);
            mIsCheater = savedInstanceState.getBoolean(KEY_CHEATER);
        }
        setContentView(R.layout.activity_quize);
        Log.i(TAG, "onCreate");
        mQuestionView = (TextView) findViewById(R.id.question);
        button1 = (Button) findViewById(R.id.true_button);
        button2 = (Button) findViewById(R.id.false_button);
        button3 = (Button) findViewById(R.id.next_button);
        button4 = (Button) findViewById(R.id.cheat_button);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "onCreate click true");
                checkAnswer(true);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG, "OnCreate click false");
                checkAnswer(false);
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrenId = (mCurrenId + 1) % mQuestionBank.length;
                updateQuestion();
                mIsCheater = false;
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (mCurrenId <= 0) {
//                    mCurrenId = mQuestionBank.length - 1;
//                } else {
//                    mCurrenId = mCurrenId - 1;
//                }
//                updateQuestion();
                Intent i = new Intent(QuizeActivity.this, CheatActivity.class);
                boolean answer = mQuestionBank[mCurrenId].ismTrueQuestion();
                i.putExtra("answer", answer);
//                startActivity(i);
                startActivityForResult(i, 1);
            }
        });
        mQuestionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrenId = (mCurrenId + 1) % mQuestionBank.length;
                updateQuestion();
            }
        });

        updateQuestion();
    }

    private void updateQuestion() {
        int question = mQuestionBank[mCurrenId].getmQuestion();
        mQuestionView.setText(question);
    }

    private void checkAnswer(boolean answer) {
        boolean answerIsTrue = mQuestionBank[mCurrenId].ismTrueQuestion();
        int messageResId = 0;
        Log.i(TAG, "checkAnswer mIsCheater:" + mIsCheater);
        if (mIsCheater == true) {
            messageResId = R.string.cheat_toast;
        } else {
            if (answer == answerIsTrue) {
                messageResId = R.string.correct_toast;
            } else {
                messageResId = R.string.incorrect_toast;
            }
        }
        Toast.makeText(QuizeActivity.this, messageResId, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                Log.i(TAG, "resultCode:" + resultCode);
                if (RESULT_OK == resultCode) {
                    boolean answer = data.getBooleanExtra("Answer", false);
                    Log.i(TAG, "answer=" + answer);
                    mIsCheater = data.getBooleanExtra("isCheat", false);
                    Log.i(TAG, "mIsCheater=" + mIsCheater);
                    //Toast.makeText(QuizeActivity.this, R.string.cheat_toast, Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
        outState.putInt(KEY_INDEX, mCurrenId);
        outState.putBoolean(KEY_CHEATER, mIsCheater);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
}
